/*
 * @Descripttion:
 * @version:
 * @Author: kl2018
 * @Date: 2020-05-28 15:51:30
 * @LastEditors: kl2018
 * @LastEditTime: 2020-08-10 10:56:14
 */
import { WeaLocaleProvider } from 'ecCom';
import { observable, action, runInAction } from 'mobx';
import { Base } from '../base';
import * as API from '../../apis/signlog/AppendSignApi';
import objectAssign from 'object-assign';

const { getLabel } = WeaLocaleProvider;

export default class AppendSelfSignLogStore extends Base {
  @observable loading = false;
  @observable signinput = {};
  @observable showSignInput = false;


  refid = 'appendSelf';
  controlOptions = false;// 是否控制选项【主要是控制节点，控制人员】
  condition = [];
  sumitParams = {}
  coreParams = { appendSelf: true }

  @action
  doInit = (params = {}, onCancel) => {
    this.coreParams = { ...this.coreParams, ...params };
    params = { ...this.coreParams, ...params };
    this.loading = false;
    this.getSignInput();
    API.odocAppendDatas(params).then((res) => {
      this.loading = true;
      const { api_status = false, signInputRequired = true } = res;
      if (api_status) {
        this.sumitParams = { signInputRequired };
      } else {
        const { api_message = '' } = res;
        this.showWarning(undefined, api_message, onCancel);
      }
    });
  }
  @action
  getSignInput = () => {
    this.showSignInput = false;
    API.getSignInput(this.coreParams).then((datas) => {
      runInAction(() => {
        this.showSignInput = true;
        this.signinput = { ...datas };
      });
    });
  }

  changeSubmitParams = (datas) => {
    this.sumitParams = objectAssign({}, this.sumitParams, datas);
  }

  doSave = (workflowStores = {}, onCancel) => {
    const { globalStore = {} } = workflowStores;
    const { submitParam = {} } = globalStore;
    let params = objectAssign({}, this.coreParams, submitParam, this.sumitParams);

    if (window.CK_REF && window.CK_REF[`${this.refid}Edit`]) {
      params.remark = window.CK_REF[`${this.refid}Edit`].getEditor().getData();
    }
    const { signInputRequired = false } = this.sumitParams;
    if (signInputRequired && !this.checkRemark(params)) { // 签字意见必填校验失败
      this.showErrorMsg(getLabel(26013, '请填写签字意见！'));
      return;
    }
    this.loading = false;
    API.saveSignSetting(params).then((res) => {
      const { api_status = false, api_message } = res;
      this.loading = true;
      if (api_status) {
        this.showSuccessMsg(api_message || (getLabel(519035, '追加意见') + getLabel(84565, '成功！')), 3, () => onCancel(res));
      } else {
        this.showErrorMsg(api_message);
      }
    });
  }

  destory = () => {
  }
}
